<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title id="assign_role">分配角色</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../static/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <!--搜索条件区域-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="aName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button  class="layui-btn layui-btn-warm"  lay-filter="data-reset-btn"><i class="layui-icon">&#xe666;</i> 重 置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <!--表格区域-->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <!--行工具栏-->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="assign"><i class="fa fa-pencil"></i>分配角色</a>
        </script>
        <!--添加修改窗口弹出层-->
        <div style="display: none;padding:5px" id="assignWindow">
            <form class="layui-form layuimini-form " id="dataFrm" lay-filter="dataFrm">
                <!--隐藏域，保存管理员id-->
                <input type="hidden" name="aId" value=""/>
                <div class="layui-form-item">
                    <label class="layui-form-label layui-disabled">管理员用户名</label>
                    <div class="layui-input-block">
                        <input type="text" name="aName"  disabled value="" class="layui-input layui-disabled">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label required">角色</label>
                    <div class="layui-input-block">
                        <select name="rId" lay-verify="required">
                            <option value="">请选择</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="../../static/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','layer','upload','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        var tableIns = table.render({
            elem: '#currentTableId',
            url: '/admin/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                {field: 'aId',  title: 'ID', sort: true},
                {field: 'aName',  title: '用户名'},
                {field: 'role',  title: '当前角色',templet:function (d) {
                        return d.role.rName;
                    }},
                // {field: 'role',  title: '角色Id',templet:function (d) {
                //         return d.role.rId;
                //     }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            done: function(res,curr,count){ //解决当一页的最后一个数据被删除后，不会跳转到前一页的问题
                //判断当前页码是否大于1且当前页数据量为0
                if(curr > 1 && res.data.length == 0){
                    var pageValue = curr - 1;
                    //刷新数据表格的数据
                    tableIns.reload({
                        page: {curr:pageValue}
                    })
                }
            }
        });

        // 监听模糊查询操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            tableIns.reload({
                page: {
                    curr: 1
                }
                , where: data.field
            });

            return false;
        });
        //监听重置操作
        form.on('click(data-reset-btn)',function () {
                $("#search-form").reset();
        });

        //保存提交地址和窗口的索引
        var url,mainIndex;
        /**
         * 打开修改窗口
         */
        function assignWindow(data){
            mainIndex = layer.open({
                title: '分配角色',
                type: 1,
                shade: 0.2,
                maxmin:true,
                shadeClose: true,
                area: ['40%', '80%'],   //宽，高
                content: $("#assignWindow"),   //引用的内容窗口
                success: function () {
                    var windowlist = {
                        "aName":data.aName,
                        "rId":data.role.rId,
                        "aId":data.aId
                    }
                    //表单数据回显
                    form.val("dataFrm",windowlist);
                }
            })
        }
        //发送请求查询角色名列表（将数据追加到下拉列表中）
        $.get("/role/getList",function (result) {
            //循环追加
            var html = "";
            for (let i = 0;i<result.length;i++){
                html += "<option value='"+result[i].rId+"'>"+result[i].rName+"</option>";
                // console.log("id="+result[i].typeId+" name="+result[i].typeName);
            }
            //将网页代码追加到下拉列表中
            $("[name='rId']").append(html);
            //重新渲染下拉列表
            form.render("select");
        },"json");

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var aId = data.aId;
            var rId = data.rId;


            //发送请求
            $.post('admin/assignRole',data.field,function (result) {
                if (result.success){
                    //成功
                    layer.alert(result.message,{icon:1});
                    //刷新当前数据表格
                    tableIns.reload();
                    //关闭当前窗口
                    layer.close(mainIndex);
                }else {
                    //失败
                    layer.alert(result.message,{icon:2});
                }
            },"json");
            return false;
        });

        //监听行工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'assign') {
                assignWindow(data);
            }
        });
    });
</script>

</body>
</html>